package com.lh.runmanage.controller;

import com.lh.runmanage.model.InPutParam.ActiveInParam;
import com.lh.runmanage.model.ProjectP5Model;
import com.lh.runmanage.model.ResultVO;
import com.lh.runmanage.service.ActiveService;
import com.lh.runmanage.service.ProjectService;
import com.lh.runmanage.unit.GetPropertiesClass;
import com.lh.runmanage.unit.ResultStruct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author ：梁昊，后端工程师：梁昊
 * @create 2021-07-13 15:20
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/activeController")
@Api(value = "项目控制层", description = "系统运行态势平台数据上传接口")
public class ActiveController {
    @Autowired
    ActiveService activeService;
    @Autowired
    GetPropertiesClass getPropertiesClass;
    @Autowired
    ProjectService projectService;

    private String projectId = null;

    /**
     * 接收上传数据，方法ID：IN2021071315191573607920388B75F
     *
     * @param id          对应tracker_id
     * @param projectCode 项目CODE
     * @param userCode    用户CODE
     * @param userName    用户名称
     * @param productCode 产品CODE
     * @param productName 产品名称
     * @param menuCode    菜单CODE
     * @param menuName    菜单名称
     * @param getDataTime 数据产生时间
     * @return 影响条数
     */
    @ApiOperation(value = "接收上传“自动”数据[影响条数],方法ID：IN2021071315191573607920388B75F", notes = "/activeController/V1/insertActive?id=id&projectCode=projectCode&userCode=userCode&userName=userName&productCode=productCode&productName=productName&menuCode=menuCode&menuName=menuName&getDataTime=getDataTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对应tracker_id, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "projectCode", value = "项目CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "userCode", value = "用户CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "userName", value = "用户名称, 长度：20", dataType = "String", required = true)
            , @ApiImplicitParam(name = "productCode", value = "产品CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "productName", value = "产品名称, 长度：50", dataType = "String", required = true)
            , @ApiImplicitParam(name = "menuCode", value = "菜单CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "menuName", value = "菜单名称, 长度：50", dataType = "String", required = true)
            , @ApiImplicitParam(name = "getDataTime", value = "数据产生时间, 长度：0", dataType = "Date", required = true)
    })
    @PostMapping("/V1/insertActiveAuto")
    public ResultVO insertActiveAuto(@RequestParam(value = "id") String id
            , @RequestParam(value = "projectCode") String projectCode
            , @RequestParam(value = "userCode") String userCode
            , @RequestParam(value = "userName") String userName
            , @RequestParam(value = "productCode") String productCode
            , @RequestParam(value = "productName") String productName
            , @RequestParam(value = "menuCode") String menuCode
            , @RequestParam(value = "menuName") String menuName
            , @RequestParam(value = "getDataTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date getDataTime) throws UnsupportedEncodingException {

        return insertActive(id, projectCode
                , userCode, userName, productCode
                , productName, menuCode, menuName
                , getDataTime, 0
                , true);
    }

    /**
     * 接收上传数据，方法ID：IN2021071315191573607920388B75F
     *
     * @param id          对应tracker_id
     * @param projectCode 项目CODE
     * @param userCode    用户CODE
     * @param userName    用户名称
     * @param productCode 产品CODE
     * @param productName 产品名称
     * @param menuCode    菜单CODE
     * @param menuName    菜单名称
     * @param getDataTime 数据产生时间
     * @return 影响条数
     */
    @ApiOperation(value = "接收上传“手动”数据[影响条数],方法ID：IN2021071315191573607920388B75F", notes = "/activeController/V1/insertActive?id=id&projectCode=projectCode&userCode=userCode&userName=userName&productCode=productCode&productName=productName&menuCode=menuCode&menuName=menuName&getDataTime=getDataTime")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "对应tracker_id, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "projectCode", value = "项目CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "userCode", value = "用户CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "userName", value = "用户名称, 长度：20", dataType = "String", required = true)
            , @ApiImplicitParam(name = "productCode", value = "产品CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "productName", value = "产品名称, 长度：50", dataType = "String", required = true)
            , @ApiImplicitParam(name = "menuCode", value = "菜单CODE, 长度：100", dataType = "String", required = true)
            , @ApiImplicitParam(name = "menuName", value = "菜单名称, 长度：50", dataType = "String", required = true)
            , @ApiImplicitParam(name = "getDataTime", value = "数据产生时间, 长度：0", dataType = "Date", required = true)
    })
    @PostMapping("/V1/insertActiveManual")
    public ResultVO insertActiveManual(@RequestParam(value = "id") String id
            , @RequestParam(value = "projectCode") String projectCode
            , @RequestParam(value = "userCode") String userCode
            , @RequestParam(value = "userName") String userName
            , @RequestParam(value = "productCode") String productCode
            , @RequestParam(value = "productName") String productName
            , @RequestParam(value = "menuCode") String menuCode
            , @RequestParam(value = "menuName") String menuName
            , @RequestParam(value = "getDataTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date getDataTime) throws UnsupportedEncodingException {
        return insertActive(id, projectCode
                , userCode, userName, productCode
                , productName, menuCode, menuName
                , getDataTime, 0
                , false);
    }

    private ResultVO insertActive(String id
            , String projectCode
            , String userCode
            , String userName
            , String productCode
            , String productName
            , String menuCode
            , String menuName
            , Date getDataTime
            , int arithCounter
            , boolean uploadType) throws UnsupportedEncodingException {
        if (projectCode != null) {
            projectCode = projectCode.trim();
            if (projectCode.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "projectCode"), ResultVO.class, null);
            }
        }
        ProjectP5Model projectP5Model = null;
        if (projectId == null || !projectId.equals(projectCode)) {
            projectP5Model = projectService.selectProjectNameByProjectId(projectCode);
        }
        if (projectP5Model == null) {
            return ResultStruct.error("无项目信息！", ResultVO.class, null);
        }
        if (userCode != null) {
            userCode = userCode.trim();
            if (userCode.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "userCode"), ResultVO.class, null);
            }
        }
        if (userName != null) {
            userName = userName.trim();
            if (userName.getBytes("gbk").length > 20) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：20", "userName"), ResultVO.class, null);
            }
        }
        if (productCode != null) {
            productCode = productCode.trim();
            if (productCode.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "productCode"), ResultVO.class, null);
            }
        }
        if (productName != null) {
            productName = productName.trim();
            if (productName.getBytes("gbk").length > 50) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：50", "productName"), ResultVO.class, null);
            }
        }
        if (menuCode != null) {
            menuCode = menuCode.trim();
            if (menuCode.getBytes("gbk").length > 100) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：100", "menuCode"), ResultVO.class, null);
            }
        }
        if (menuName != null) {
            menuName = menuName.trim();
            if (menuName.getBytes("gbk").length > 50) {
                return ResultStruct.error(String.format("字段【%s】超长，最大长度为：50", "menuName"), ResultVO.class, null);
            }
        }

        ActiveInParam activeInParam = new ActiveInParam();
        activeInParam.setId(id);
        activeInParam.setProjectCode(projectCode);
        activeInParam.setProjectName(projectP5Model.getProjName());
        activeInParam.setUserCode(userCode);
        activeInParam.setUserName(userName);
        activeInParam.setProductCode(productCode);
        activeInParam.setProductName(productName);
        activeInParam.setMenuCode(menuCode);
        activeInParam.setMenuName(menuName);
        activeInParam.setGetDataTime(getDataTime);
        activeInParam.setArithCounter(arithCounter);
        activeInParam.setUploadType(uploadType);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        activeInParam.setSeparateId(formatter.format(getDataTime));
        int resultCount = activeService.insertActive(activeInParam);
        if (resultCount > 0)
            return ResultStruct.success(resultCount);
        else if (resultCount == -1000)
            return ResultStruct.error("重复，不允许增加", ResultVO.class, int.class);
        else
            return ResultStruct.error("增加失败", ResultVO.class, int.class);
    }

    /**
     * 删除历史数据，方法ID：DE20210713153548221A70A332598F5
     *
     * @return 影响条数
     */
    @ApiOperation(value = "调度系统接口：删除历史数据[影响条数],方法ID：DE20210713153548221A70A332598F5", notes = "/activeController/V1/deleteActiveFromCreate?separateId=separateId&getDataTime=getDataTime")
    @PostMapping("/V1/deleteActiveFromCreate")
    public int deleteActiveFromCreate() {
        Date date = new Date();//当前日期
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        final int deleteMonths = getPropertiesClass.getDeleteMonths() * -1;
        calendar.add(calendar.DAY_OF_MONTH, deleteMonths); //把日期往后增加一天,整数  往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果

        ActiveInParam activeInParam = new ActiveInParam();
        activeInParam.setGetDataTime(date);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
        activeInParam.setSeparateId(formatter.format(date));
        return activeService.deleteActiveFromCreate(activeInParam);
    }

}